﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class ContactDepartment
    {
         #region Common
        public eContactDepartment   Data;

        public ContactDepartment()
        {
            Data = new eContactDepartment();
		}

        ~ContactDepartment()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cContactDepartment objDAL = new cContactDepartment();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cContactDepartment objDAL = new cContactDepartment();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cContactDepartment objInfor = new cContactDepartment(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.ContactDepartmentID;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cContactDepartment objInfor = new cContactDepartment();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

      

        private void MappingData(cContactDepartment objInfor)
        {
            objInfor.ContactOfficeID = Data.ContactOfficeID;

            try { objInfor.DepartmentName1 = Data.DepartmentName1; }
            catch { objInfor.DepartmentName1 =""; }

            try { objInfor.DepartmentName2 = Data.DepartmentName2; }
            catch { objInfor.DepartmentName2 = ""; }

            try { objInfor.DepartmentName3 = Data.DepartmentName3; }
            catch { objInfor.DepartmentName3 = ""; }

            try { objInfor.Email = Data.Email; }
            catch { objInfor.Email = ""; }

            try { objInfor.Tel1 = Data.Tel1; }
            catch { objInfor.Tel1 = ""; }

            try { objInfor.Tel2 = Data.Tel2; }
            catch { objInfor.Tel2 = ""; }

            try { objInfor.Tel3 = Data.Tel3; }
            catch { objInfor.Tel3 = ""; }
        }

        private void ConvertData(cContactDepartment objInfor)
        {
            Data.ContactOfficeID = objInfor.ContactOfficeID;
            Data.DepartmentName1 = objInfor.DepartmentName1;
            Data.DepartmentName2 = objInfor.DepartmentName2;
            Data.DepartmentName3 = objInfor.DepartmentName3;
            Data.Email = objInfor.Email;
            Data.Tel1 = objInfor.Tel1;
            Data.Tel2 = objInfor.Tel2;
            Data.Tel3 = objInfor.Tel3;
        }
        #endregion

        public DataTable Search(int ContactOfficeID)
        {
            DataTable tbResult = new DataTable();
            cContactDepartment objDAL = new cContactDepartment();
            tbResult = objDAL.Search(ContactOfficeID);
            return tbResult;
        } 
    }
}
